package com.cloudcc.mobile.parser;

import org.json.JSONArray;
import org.json.JSONObject;

import com.cloudcc.mobile.entity.UserMessage;
import com.cloudcc.mobile.entity.addmicropost.AddMircReturnData;
import com.cloudcc.mobile.entity.comment.RemoveMicroCommentMessage;
import com.cloudcc.mobile.entity.comment.RemoveMicroPostMessage;
import com.cloudcc.mobile.entity.handle.FavoriteFeedInfo;
import com.cloudcc.mobile.entity.handle.PraiseFeedInfo;
import com.cloudcc.mobile.entity.handle.PraisefeedData;
import com.cloudcc.mobile.entity.handle.VoteInfo;
import com.cloudcc.mobile.util.Json;
import com.cloudcc.mobile.util.Tools;

public class HandleParser {
	/** * 解析"喜欢"返回的数据 */
	public static PraiseFeedInfo xihuan(String jsonString) {
		PraiseFeedInfo info = new PraiseFeedInfo();
		try {
			JSONObject obj = new JSONObject(jsonString);
			info.setResult(obj.getBoolean("result"));
			info.setReturnCode(obj.getString("returnCode"));
			info.setReturnInfo(obj.getString("returnInfo"));
			JSONObject p = obj.getJSONObject("data");
			PraisefeedData praisefeedData = (PraisefeedData) Json.toObject(
					p.toString(), PraisefeedData.class);
			info.setData(praisefeedData);
		} catch (Exception e) {
			Tools.handle(e);
		}
		return info;
	}

	/** * 解析"删除帖子"返回的数据 */
	public static RemoveMicroPostMessage removetiezi(String jsonString) {
		RemoveMicroPostMessage msg = new RemoveMicroPostMessage();
		try {
			JSONObject obj = new JSONObject(jsonString);
			msg.setResult(obj.getBoolean("result"));
			msg.setReturnCode(obj.getString("returnCode"));
			msg.setReturnInfo(obj.getString("returnInfo"));
		} catch (Exception e) {
			Tools.handle(e);
		}
		return msg;
	}

	/** * 解析"收藏"返回的数据 */
	public static FavoriteFeedInfo shoucang(String jsonString) {
		FavoriteFeedInfo info = new FavoriteFeedInfo();
		try {
			JSONObject obj = new JSONObject(jsonString);
			info.setResult(obj.getBoolean("result"));
			info.setReturnCode(obj.getString("returnCode"));
			info.setReturnInfo(obj.getString("returnInfo"));
			// JSONObject chatuser=obj.getJSONObject("data");
			// ChatUser user=(ChatUser) Json.toObject(chatuser.toString(),
			// ChatUser.class);
			// info.setChatuser(user);
		} catch (Exception e) {
			Tools.handle(e);
		}
		return info;
	}

	/** * 解析"删除评论"返回的数据 */
	public static RemoveMicroCommentMessage removecomment(String jsonString) {
		RemoveMicroCommentMessage msg = new RemoveMicroCommentMessage();
		try {
			JSONObject obj = new JSONObject(jsonString);
			msg.setResult(obj.getBoolean("result"));
			msg.setReturnCode(obj.getString("returnCode"));
			msg.setReturnInfo(obj.getString("returnInfo"));
			JSONObject addmicro = obj.getJSONObject("data");
			Tools.i("addmicro", addmicro.toString());
			AddMircReturnData post = (AddMircReturnData) Json.toObject(
					addmicro.toString(), AddMircReturnData.class);
			msg.setId(post.getId());
		} catch (Exception e) {
			Tools.handle(e);
		}
		return msg;
	}

	/** * 解析"留言"返回的数据 */
	public static UserMessage liuyan(String jsonString) {
		return null;
	}

	/** * 解析"正文页面投票"返回的数据 */
	public static VoteInfo vote(String jsonstring) {
		VoteInfo msg = new VoteInfo();
		try {
			JSONObject obj = new JSONObject(jsonstring);
			msg.setResult(obj.getBoolean("result"));
			msg.setReturnCode(obj.getString("returnCode"));
			msg.setReturnInfo(obj.getString("returnInfo"));
		} catch (Exception e) {
			Tools.handle(e);
		}
		return msg;
	}

	public static String insertLeadInfo(String jsonstring) {
		String id = "";
		try {
			/**
			 * {"result":true,"returnInfo":"","returnCode":"1","data":{"ids":[{
			 * "id":"004201448411930noW2D"}]}}
			 */
			JSONObject obj = new JSONObject(jsonstring);
			JSONObject oo = obj.getJSONObject("data");
			JSONArray ids = oo.getJSONArray("ids");
			JSONObject jj = ids.getJSONObject(0);
			id = jj.getString("id");
		} catch (Exception e) {
			Tools.handle(e);
		}
		return id;
	}

}
